package com.user.server;

import com.common.utils.JwtUtils;
import com.common.utils.Result;

import com.common.utils.TheadPoolUtils;
import com.user.mapper.LoginMapper;
import com.user.pojo.dto.LoginDto;
import com.user.pojo.entity.User;
import com.user.pojo.vo.LoginVo;
import com.user.server.impl.LoginServerImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class LoginServer implements LoginServerImpl {

    @Resource
    private LoginMapper loginMapper;

    @Override
    public Result checkout(LoginDto loginDto) {
       User user= loginMapper.selectId(loginDto);
       if (user==null){
           return Result.error("不存在该用户");
       }

        LoginVo loginVo = new LoginVo();

//      生成短token
        Date date = new Date(System.currentTimeMillis()+2*60*1000);
        String shortToken = JwtUtils.getToken(user.getUserId(), date);
        loginVo.setShortToken("s"+shortToken);
//       生成刷新token
        Date date1 = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
        String longToken = JwtUtils.getToken(user.getUserId(), date1);
        loginVo.setLongToken("l"+longToken);

        BeanUtils.copyProperties(user,loginVo);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date a= new Date(System.currentTimeMillis());
        String format = simpleDateFormat.format(a);
        TheadPoolUtils.getPool().execute(() -> {
            System.out.println(user.getUserId());
            System.out.println(format);
            loginMapper.insertNum(user.getUserId(),format);
        });
        return Result.success(loginVo);
    }
}
